﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>
    layui.use(['commonTable', 'form', 'table', 'common', 'optimizeSelectOption'], function () {
        var commonTable = layui.commonTable,
            form = layui.form,
            table = layui.table,
            common = layui.common;
        wcLoading.close();
        var dataItems = {
            "0": "Job",
            "1": "Get",
            "2": "Post",
            "3": "Put",
            "4": "Delete",
            "5": "Sql"
        };
        form.render();
        commonTable.rendertable({
            elem: '#currentTableId',
            id: 'currentTableId',
            url: '/SystemSecurity/OpenJobs/GetGridJson',
            defaultToolbar: [],//默认工具栏
            search: true,//搜索按钮
            cols: [[
                { type: "radio", width: 50, fixed: 'left' },
                { field: 'JobName', title: '名称', minWidth: 180, sort: true },
                { field: 'JobGroup', title: '组名', minWidth: 120, sort: true },
                { field: 'IsLog', title: '是否记录日志', width: 130, sort: true,hide:true },
                {
                    field: 'JobType', title: '类型', width: 80, sort: true,
                    templet: function (d) {
                        return dataItems[d.JobType] == undefined ? "" : dataItems[d.JobType];
                    }
                },
                {
                    field: 'EnabledMark', title: '任务状态', width: 105, sort: true,
                    templet: function (d) {
                        if (d.EnabledMark == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>有效</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>无效</span>";
                        }
                    }
                },
                { field: 'CronExpress', title: 'CRON', width: 130 },
                {
                    field: 'LastRunMark', title: '最近执行状态', width: 130, sort: true,
                    templet: function (d) {
                        if (d.LastRunMark == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>成功</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>失败</span>";
                        }
                    }
                },
                { field: 'LastRunTime', title: '最近执行时间', width: 160 },
                { field: 'NextValidTimeAfter', title: '下次执行时间', width: 160 },
                { field: 'LastRunErrTime', title: '上次发生错误时间', width: 160},
                { field: 'LastRunErrMsg', title: '上次发生错误信息', width: 180},
                { field: 'StarRunTime', title: '最近启动时间', width: 170,hide:true },
                { field: 'EndRunTime', title: '最近关闭时间', width: 170,hide:true },
                { field: 'FileName', title: '文件名', width: 180,hide:true },
                { field: 'RequestUrl', title: '请求地址', width: 180,hide:true},
                { field: 'RequestHeaders', title: '请求头', width: 180,hide:true},
                { field: 'RequestString', title: '请求参数', width: 180,hide:true},
                { field: 'JobDBProvider', title: '执行数据库', width: 180  ,hide:true},
                { field: 'JobSql', title: '执行SQL', width: 180,hide:true},
                { field: 'JobSqlParm', title: 'Sql参数', width: 180 ,hide:true },
                { field: 'Description', title: '备注', width: 150,hide:true }

            ]]
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            commonTable.reloadtable({
                elem: 'currentTableId',
                page: false,
                curr: 1,
                where: { keyword: data.field.txt_keyword }
            });
            return false;
        });
        //行点击事件监听，控制按钮显示
        var oneList = ["NF-edit", "NF-delete", "NF-disabled", "NF-enabled", "NF-edit", "NF-log", "NF-doNow"];//选择1条显示
        commonTable.tableRowClick("radio", "currentTableFilter", "currentTableId", oneList);
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            var data = table.checkStatus('currentTableId').data;
            if (obj.event === 'add') {  // 监听删除操作
                common.modalOpen({
                    title: "添加任务",
                    url: "/SystemSecurity/OpenJobs/Form",
                    width: "750px",
                    height: "650px",
                });
            }
            else if (obj.event === 'delete') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.deleteForm({
                    url: "/SystemSecurity/OpenJobs/DeleteForm",
                    param: { keyValue: data[0].Id },
                    success: function () {
                        common.reload('data-search-btn');
                    }
                });
            }
            else if (obj.event === 'edit') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.modalOpen({
                    title: "修改任务",
                    url: "/SystemSecurity/OpenJobs/Form?keyValue=" + data[0].Id,
                    width: "750px",
                    height: "650px",
                });
            }
            else if (obj.event === 'clear') {
                common.submitPost({
                    prompt: "注：您确定要【清除所有任务计划】吗？",
                    url: "/SystemSecurity/OpenJobs/ClearScheduleJob",
                    success: function () {
                        common.reload('data-search-btn');
                    }
                });
            }
            else if (obj.event === 'doNow') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.submitPost({
                    prompt: "注：您确定要【执行】该项任务吗？",
                    url: "/SystemSecurity/OpenJobs/DoNow",
                    param: { keyValue: data[0].Id },
                    success: function () {
                        common.reload('data-search-btn');
                    }
                });
            }
            else if (obj.event === 'disabled') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                if (data[0].EnabledMark != true) {
                    common.modalMsg("任务未启动，无法关闭！", "warning");
                    return false;
                }
                common.submitPost({
                    prompt: "注：您确定要【关闭】该项任务吗？",
                    url: "/SystemSecurity/OpenJobs/ChangeStatus",
                    param: { keyValue: data[0].Id, status: 0 },
                    success: function () {
                        common.reload('data-search-btn');
                    }
                });
            }
            else if (obj.event === 'enabled') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                if (data[0].EnabledMark == true) {
                    common.modalMsg("任务已启动，无法启动！", "warning");
                    return false;
                }
                common.submitPost({
                    prompt: "注：您确定要【启动】该项任务吗？",
                    url: "/SystemSecurity/OpenJobs/ChangeStatus",
                    param: { keyValue: data[0].Id, status: 1 },
                    success: function () {
                        common.reload('data-search-btn');
                    }
                });
            }
            else if (obj.event === 'log') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.modalOpen({
                    title: "任务日志",
                    url: "/SystemSecurity/OpenJobs/Details?keyValue=" + data[0].Id,
                    width: "700px",
                    height: "600px",
                    btn: []
                });
            }
            else if (obj.event === 'TABLE_SEARCH') {
                var _that = $("#searchField");
                if (_that.hasClass("layui-hide")) {
                    _that.removeClass('layui-hide');
                } else {
                    _that.addClass('layui-hide');
                }
            }
            return false;
        });
    });
</script>
<div class="layuimini-container">
    <div class="layuimini-main">     
        <fieldset class="table-search-fieldset layui-hide" id="searchField">
            @*<legend>搜索信息</legend>*@
            <div>
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字:</label>
                            <div class="layui-input-inline">
                                <input type="text" id="txt_keyword" name="txt_keyword" autocomplete="off" class="layui-input" placeholder="">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon">&#xe615;</i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container" id="toolbar">
@*                <button id="NF-clear" name="NF-clear" authorize class="layui-btn layui-btn-sm data-add-btn" lay-event="clear"><i class="layui-icon">&#xe665;</i>清除任务计划</button>*@
                <button id="NF-add" name="NF-add" authorize class="layui-btn layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</button>
                <button id="NF-edit" name="NF-edit" authorize class="layui-btn layui-btn-sm layui-btn-warm data-edit-btn" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</button>
                <button id="NF-delete" name="NF-delete" authorize class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> <i class="layui-icon">&#xe640;</i>删除</button>
                <button id="NF-doNow" name="NF-doNow" authorize class="layui-btn layui-btn-sm layui-btn-warm" lay-event="doNow"><i class="layui-icon">&#xe609;</i>执行</button>
                <button id="NF-enabled" name="NF-enabled" authorize class="layui-btn layui-btn-sm data-enabled-btn" lay-event="enabled"> <i class="fa fa-play-circle"></i>启动</button>
                <button id="NF-disabled" name="NF-disabled" authorize class="layui-btn layui-btn-sm layui-btn-danger data-disabled-btn" lay-event="disabled"><i class="fa fa-stop-circle"></i>关闭</button>
                <button id="NF-log" name="NF-log" authorize class="layui-btn layui-btn-sm data-log-btn" lay-event="log"><i class="layui-icon">&#xe702;</i>日志</button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    </div>
</div>